﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using coffeeshop.coffeeDataSetTableAdapters;
using System.Data;

namespace coffeeshop.Models
{
    public class ProductInventory
    {
        public int ProductId { get; set; }
        public double ReceiptQuantity { get; set; }
        public double InstockQuantity { get; set; }
        public DateTime CreatedDate { get; set; }
        public string UnitName { get; set; }
        public int Price { get; set; }
        public string Name { get; set; }
        public int CompoundProductID { get; set; }
        public static int index { get; set; }
        private static ProductInventoryTableAdapter _adapter = new ProductInventoryTableAdapter();
        public ProductInventory() { }

        public static DataTable getListProduct()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = _adapter.GetProductInventory();
                return dt;
            }
            catch (System.Data.ConstraintException)
            {
                return dt;
            }
        }

        public bool insertWarehouse()
        {
            bool insertSuccess = false;

            _adapter.Connection.Open();
            _adapter.Transaction = _adapter.Connection.BeginTransaction();
            try
            {
                _adapter.UpdateInventory(ProductId, ReceiptQuantity, InstockQuantity, null);
                _adapter.Transaction.Commit();
                insertSuccess = true;
            }
            catch
            {
                _adapter.Transaction.Rollback();
            }
            finally
            {
                _adapter.Connection.Close();
            }

            return insertSuccess;
        }

        public static List<ProductInventory> getWarehouseHistorical()
        {
            List<ProductInventory> lstProductInventory = new List<ProductInventory>();
            try
            {
                DataTable dt = new DataTable();
                dt = _adapter.GetWarehouseHistorical();
                foreach (DataRow row in dt.Rows)
                {
                    ProductInventory pi = new ProductInventory();
                    pi.ProductId = int.Parse(row["ProductId"].ToString());
                    pi.Name = row["Name"].ToString();
                    pi.CreatedDate = DateTime.Parse(row["CreatedDate"].ToString());
                    lstProductInventory.Add(pi);
                }

                return lstProductInventory;
            }
            catch (System.Data.ConstraintException)
            {
                return lstProductInventory;
            }
        }

        public static DataTable getWarehouseHisDetail(DateTime _CreatedDate)
        {
            try
            {
                DataTable dt = new DataTable();

                dt = _adapter.GetWarehouseHisDetail(_CreatedDate);
                return dt;
            }
            catch (System.Data.ConstraintException)
            {
                return new DataTable();
            }
        }

    }
}
